//
//  MTClubDetailAvatorCell.swift
//  MT
//
//  Created by ooops on 2019/12/27.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import Kingfisher

class MTClubDetailAvatorCell: UICollectionViewCell {
    
    
    override func prepareForReuse() {
        super.prepareForReuse()
        self.avatorImageView.kf.cancelDownloadTask()
    }
    
    private lazy var userDefaultIcon = UIImage(named: defaultPlaceholderIcon60x60)
    
    class func reuseIdentity() -> String {
        return "MTClubDetailAvatorCellId"
    }
    
    var avator: UIImage? {
        didSet {
            avatorImageView.image = nil
            avatorImageView.image = avator
        }
    }
    
    func bindDate(user: JMSGUser) {
        
        if let avator = user.avatar {
            self.avatorImageView.kf.setImage(with: URL(string: avator), placeholder: self.userDefaultIcon)
        }
        
//        user.thumbAvatarData { (data, id, error) in
//            if let data = data {
//                let image = UIImage(data: data)
//                self.avatorImageView.image = image
//            } else {
//                self.avatorImageView.image = self.userDefaultIcon
//            }
//        }
    }
    
//    func bindDate(user: MTClubActDetailUserModel) {
//        if let avator = user.href {
//            self.avatorImageView.kf.setImage(with: URL(string: avator))
//        } else {
//            self.avatorImageView.image = self.userDefaultIcon
//        }
//    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
    }
    
    private func setupUI() {
        contentView.addSubview(avatorImageView)
    }
    
    private func makeConstraint() {
        avatorImageView.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview()
            make.width.height.equalToSuperview()
            make.bottom.equalToSuperview().priority(.low)
        }
    }
    
    lazy var avatorImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFill
        imageV.layer.cornerRadius = 22
        imageV.layer.masksToBounds = true
        imageV.image = UIImage(named: defaultPlaceholderIcon60x60)
        return imageV
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        avatorImageView.layer.cornerRadius = self.height / 2
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
}
